<template>
  <div class="app-container">

        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
          label-width="68px">
          <el-form-item label="时间范围" prop="dateRange">
            <el-date-picker
              v-model="dateRange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
              style="width: 300px"
              @change="handleDateRangeChange"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="姓名" prop="createTime">
            <el-input v-model="queryParams.username" placeholder="请输入姓名" clearable style="width: 200px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="openlockList" max-height="500">
          <el-table-column label="名字" align="center" prop="username" />
          <el-table-column label="学工号" align="center" prop="idCardNumber" />
        <!--  <el-table-column label="类型" align="center" prop="relation">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.relation" :value="scope.row.relation" />
            </template>
          </el-table-column> -->
		  <el-table-column label="开门时间" align="center" prop="createTime" width="180" />

          <el-table-column label="开门方式" align="center" prop="type">

        <template slot-scope="scope">
          <dict-tag :options="dict.type.key_type" :value="scope.row.type" />
        </template>
      </el-table-column>
	   <el-table-column label="备注" align="center" prop="remark" width="210" />
        </el-table>
        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />

  </div>
</template>

<script>
  import {
    listOpenlock,
    getOpenlock,
    delOpenlock,
    addOpenlock,
    updateOpenlock
  } from "@/api/lock/openlock";

  export default {
    name: "Openlock",
      dicts: ['relation','key_type'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 开门记录表格数据
        openlockList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 校区选项
        cellOptions: [],
        // 楼栋选项
        unitOptions: [],
        // 楼层选项
        floorOptions: [],
        // 房间选项
        roomOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          mac: null,
          createTime: null,
          action: null,
          phone: null,
          type: null,
          cellId: null,
          unitId: null,
          floorId: null,
          roomId: null,
          username: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        // 位置级联选择器的值
        locationIds: [],

        // 位置级联选择器的选项
        locationOptions: [],
        // 日期范围
        dateRange: [],
      };
    },
    created() {
      this.queryParams.homeId = this.$route.query.id;

      this.getList();
    },
    methods: {
      /** 查询开门记录列表 */
      getList() {
        this.loading = true;
        const params = { ...this.queryParams };

        // 设置日期范围参数
        if (this.dateRange && this.dateRange.length === 2) {
          params.beginTime = this.dateRange[0];
          params.endTime = this.dateRange[1];
        }

        // 查询开门记录
        listOpenlock(params).then(response => {
          this.openlockList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },

      /** 搜索条件校区选择改变时触发 */
      handleQueryCellChange(value) {
        this.getUnitList(value);
      },
      /** 搜索条件楼栋选择改变时触发 */
      handleQueryUnitChange(value) {
        this.getFloorList(value);
      },
      /** 搜索条件楼层选择改变时触发 */
      handleQueryFloorChange(value) {
        const selectedFloor = this.floorOptions.find(f => f.id === value);
        if (selectedFloor) {
          this.queryParams.floorName = selectedFloor.name;
        }
        this.getRoomList(value);
      },
      /** 表单校区选择改变时触发 */
      handleCellChange(value) {
        this.getUnitList(value);
      },
      /** 表单楼栋选择改变时触发 */
      handleUnitChange(value) {
        this.getFloorList(value);
      },

      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          mac: null,
          logId: null,
          createTime: null,
          trackId: null,
          action: null,
          keyId: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          phone: null,
          type: null,
          cellId: null,
          unitId: null,
          floorId: null,
          roomId: null,
          username: null
        };
        this.locationIds = [];
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },



      /** 导出按钮操作 */
      handleExport() {
        this.download('lock/openlock/export', {
          ...this.queryParams
        }, `openlock_${new Date().getTime()}.xlsx`)
      },

      /** 日期范围变化事件 */
      handleDateRangeChange(value) {
        this.dateRange = value;
      },
    }
  };
</script>
